from lxml import etree
import requests as rs
import os, base64
import time

PICTURES_PATH=os.path.join(os.getcwd(),'pictures\\')

headers ={  
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36',  
}

def  GetUrl(num):
    url='http://wuxianfuli.cc/pic/?paged={}'.format(num)
    Spider1(url)

def Spider1(url):
    
    html=rs.get(url ,timeout=15,headers= headers).text
    #print(html)
    s=etree.HTML(html)
    # 获取图片 

    imageurl = s.xpath('//div/p/img/@data-cm-src')[0] 
    print(imageurl) 
    if imageurl is not None:
        download_pic(imageurl)
    else:
        pass
    # 获取下一页连接
    nexturl= s.xpath('//nav/div/a[@class="next page-numbers"]/@href')[0]
    if nexturl is not None:
        time.sleep(2)
        Spider1(nexturl)
    else:
        pass
 
    
  
def download_pic(imageurl):           
    try:
        os.makedirs(PICTURES_PATH)
    except: 
        pass   
    img_data=rs.get(imageurl, timeout=15,headers= headers )
    #print(img_data.content)
    img_name=str(int(time.time() * 1000))
    pic_path=PICTURES_PATH+'\\'+img_name+imageurl[-4:]
    print(pic_path)
    if os.path.isfile(pic_path):
        print('{}已存在'.format(img_name))
        pass
    else:
        with open(pic_path,'wb') as f:
            print(img_data.content)
            f.write(img_data.content)                 
            f.close
            print('正在保存{}'.format(img_name))
    
    time.sleep(1)
    return 


if __name__  == '__main__' : 
    GetUrl(1)
    

     
